﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
    /// <summary>
    /// 数据访问类:wx_shop_user_addr
    /// </summary>
    public partial class wx_shop_user_addr
    {
        public wx_shop_user_addr()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("id", "wx_shop_user_addr");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from wx_shop_user_addr");
            strSql.Append(" where id=@id ");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32,11)			};
            parameters[0].Value = id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Maticsoft.Model.wx_shop_user_addr model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into wx_shop_user_addr(");
            strSql.Append("id,wid,openid,province,city,area,addrDetail,tel,jiedao,contractPerson,createDate,isDefault)");
            strSql.Append(" values (");
            strSql.Append("@id,@wid,@openid,@province,@city,@area,@addrDetail,@tel,@jiedao,@contractPerson,@createDate,@isDefault)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32,11),
					new MySqlParameter("@wid", MySqlDbType.Int32,11),
					new MySqlParameter("@openid", MySqlDbType.VarChar,100),
					new MySqlParameter("@province", MySqlDbType.VarChar,50),
					new MySqlParameter("@city", MySqlDbType.VarChar,50),
					new MySqlParameter("@area", MySqlDbType.VarChar,100),
					new MySqlParameter("@addrDetail", MySqlDbType.VarChar,200),
					new MySqlParameter("@tel", MySqlDbType.VarChar,50),
					new MySqlParameter("@jiedao", MySqlDbType.VarChar,100),
					new MySqlParameter("@contractPerson", MySqlDbType.VarChar,30),
					new MySqlParameter("@createDate", MySqlDbType.DateTime),
					new MySqlParameter("@isDefault", MySqlDbType.VarChar,1)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.wid;
            parameters[2].Value = model.openid;
            parameters[3].Value = model.province;
            parameters[4].Value = model.city;
            parameters[5].Value = model.area;
            parameters[6].Value = model.addrDetail;
            parameters[7].Value = model.tel;
            parameters[8].Value = model.jiedao;
            parameters[9].Value = model.contractPerson;
            parameters[10].Value = model.createDate;
            parameters[11].Value = model.isDefault;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Maticsoft.Model.wx_shop_user_addr model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update wx_shop_user_addr set ");
            strSql.Append("wid=@wid,");
            strSql.Append("openid=@openid,");
            strSql.Append("province=@province,");
            strSql.Append("city=@city,");
            strSql.Append("area=@area,");
            strSql.Append("addrDetail=@addrDetail,");
            strSql.Append("tel=@tel,");
            strSql.Append("jiedao=@jiedao,");
            strSql.Append("contractPerson=@contractPerson,");
            strSql.Append("createDate=@createDate,");
            strSql.Append("isDefault=@isDefault");
            strSql.Append(" where id=@id ");
            MySqlParameter[] parameters = {
					new MySqlParameter("@wid", MySqlDbType.Int32,11),
					new MySqlParameter("@openid", MySqlDbType.VarChar,100),
					new MySqlParameter("@province", MySqlDbType.VarChar,50),
					new MySqlParameter("@city", MySqlDbType.VarChar,50),
					new MySqlParameter("@area", MySqlDbType.VarChar,100),
					new MySqlParameter("@addrDetail", MySqlDbType.VarChar,200),
					new MySqlParameter("@tel", MySqlDbType.VarChar,50),
					new MySqlParameter("@jiedao", MySqlDbType.VarChar,100),
					new MySqlParameter("@contractPerson", MySqlDbType.VarChar,30),
					new MySqlParameter("@createDate", MySqlDbType.DateTime),
					new MySqlParameter("@isDefault", MySqlDbType.VarChar,1),
					new MySqlParameter("@id", MySqlDbType.Int32,11)};
            parameters[0].Value = model.wid;
            parameters[1].Value = model.openid;
            parameters[2].Value = model.province;
            parameters[3].Value = model.city;
            parameters[4].Value = model.area;
            parameters[5].Value = model.addrDetail;
            parameters[6].Value = model.tel;
            parameters[7].Value = model.jiedao;
            parameters[8].Value = model.contractPerson;
            parameters[9].Value = model.createDate;
            parameters[10].Value = model.isDefault;
            parameters[11].Value = model.id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from wx_shop_user_addr ");
            strSql.Append(" where id=@id ");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32,11)			};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from wx_shop_user_addr ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.wx_shop_user_addr GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,wid,openid,province,city,area,addrDetail,tel,jiedao,contractPerson,createDate,isDefault from wx_shop_user_addr ");
            strSql.Append(" where id=@id ");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32,11)			};
            parameters[0].Value = id;

            Maticsoft.Model.wx_shop_user_addr model = new Maticsoft.Model.wx_shop_user_addr();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.wx_shop_user_addr DataRowToModel(DataRow row)
        {
            Maticsoft.Model.wx_shop_user_addr model = new Maticsoft.Model.wx_shop_user_addr();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["wid"] != null && row["wid"].ToString() != "")
                {
                    model.wid = int.Parse(row["wid"].ToString());
                }
                if (row["openid"] != null)
                {
                    model.openid = row["openid"].ToString();
                }
                if (row["province"] != null)
                {
                    model.province = row["province"].ToString();
                }
                if (row["city"] != null)
                {
                    model.city = row["city"].ToString();
                }
                if (row["area"] != null)
                {
                    model.area = row["area"].ToString();
                }
                if (row["addrDetail"] != null)
                {
                    model.addrDetail = row["addrDetail"].ToString();
                }
                if (row["tel"] != null)
                {
                    model.tel = row["tel"].ToString();
                }
                if (row["jiedao"] != null)
                {
                    model.jiedao = row["jiedao"].ToString();
                }
                if (row["contractPerson"] != null)
                {
                    model.contractPerson = row["contractPerson"].ToString();
                }
                if (row["createDate"] != null && row["createDate"].ToString() != "")
                {
                    model.createDate = DateTime.Parse(row["createDate"].ToString());
                }
                if (row["isDefault"] != null)
                {
                    model.isDefault = row["isDefault"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,wid,openid,province,city,area,addrDetail,tel,jiedao,contractPerson,createDate,isDefault ");
            strSql.Append(" FROM wx_shop_user_addr ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM wx_shop_user_addr ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from wx_shop_user_addr T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "wx_shop_user_addr";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

